<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%--
  Created by IntelliJ IDEA.
  User: fzh
  Date: 2017/9/13
  Time: 21:13
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<!doctype html>
<html>
<head>
    <title>首页</title>
    <meta charset="UTF-8">
    <meta name="viewport"
          content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
    <link rel="stylesheet" href="${pageContext.request.contextPath}/assets/css/default.css">
    <script type="text/javascript" src="${pageContext.request.contextPath}/assets/js/jquery-3.2.1.min.js"></script>
    <style type="text/css">
        .container {
            display: flex;
            box-sizing: border-box;
            justify-content: center;
            padding-top: 10px;
            padding-bottom: 10px;
            align-items: center;
            width: 100%;
            min-height: calc(100% - 212px);
        }

        .container form {
            width: 40%;
            display: flex;
            flex-direction: column;
        }

        .container form .row {
            display: flex;
            width: 100%;
        }

        .container form .row .label {
            text-align: right;
            padding-right: 10px;
            width: 20%;
        }

        .container form .row .content {
            display: flex;
            flex-direction: column;
            width: 70%;
            min-height: 40px;
        }

        .container form .row .content input,
        .container form .row .content label {
            width: 100%;
        }

        .container form .row .content button {
            position: relative;
            width: 120px;
            height: 25px;
            box-sizing: border-box;
            border: none;
            outline: none;
            cursor: pointer;
            color: #FFFFFF;
            background-color: #11C632;
        }

        .container form .row .content button:hover,
        .container form .row .content button:focus {
            background-color: rgba(17, 198, 50, 0.8);
        }
        .container form .row .content button:focus{
            top: 1px;
        }

        .error-msg {
            font-size: 12px;
            color: #EF6C35;
        }
    </style>
</head>
<body>
<jsp:include page="../components/pageHeader.jsp"/>
<div class="container">
    <form name="registerForm" action="<c:url value="/user/doRegister"/>" method="post" autocomplete="off">
        <div class="row">
            <div class="label">用户名</div>
            <div class="content">
                <input id="un" title="用户名" type="text" name="username" required="required" onblur="fetchUn()" onfocus="clearLabel(this.id)">
                <label id="error_un" class="error-msg"></label>
            </div>
        </div>
        <div class="row">
            <div class="label">密码</div>
            <div class="content">
                <input id="pw" title="密码" type="password" name="password" required="required" onfocus="clearLabel(this.id)">
                <label id="error_pw" class="error-msg"></label>
            </div>
        </div>
        <div class="row">
            <div class="label">确认密码</div>
            <div class="content">
                <input id="pw2" title="再次输入确认密码" type="password" required="required" onfocus="clearLabel(this.id)">
                <label id="error_pw2" class="error-msg"></label>
            </div>
        </div>
        <div class="row">
            <div class="label">手机号码</div>
            <div class="content">
                <input id="phone" title="手机号码" name="phone" required="required" onfocus="clearLabel(this.id)">
                <label id="error_phone" class="error-msg"></label>
            </div>
        </div>
        <div class="row">
            <div class="label"></div>
            <div class="content">
                <button type="submit" onclick="preSubmit(event)">提交</button>
            </div>
        </div>
    </form>
</div>
<jsp:include page="../components/pageFooter.jsp"/>

<script type="text/javascript">
    function clearLabel(id) {
        $("#error_"+id).text("");
    }
    function preSubmit(event) {
        check().catch(() => {
            event.preventDefault();
        });
    }

    function check() {
        return new Promise((resolve, reject) => {
            if (unCheck() && pwCheck() && phoneCheck()) {
                resolve();
            } else {
                reject();
            }
        });
    }

    function unCheck() {
        let un = $("#un");
        let unv = un.val();
        if (unv.length < 6 || unv.length > 20) {
            un.focus();
            $("#error_un").text("用户名长度应在 6-20 之间");
            return false;
        }
        return fetchUn();

    }

    function pwCheck() {
        let pw = $("#pw");
        let pwv = pw.val();
        let pw2 = $("#pw2");
        let pwv2 = pw2.val();
        if (pwv.length < 6 || pwv2.length > 32) {
            pw.focus();
            $("#error_pw").text("密码长度应在 6-32 之间");
            return false;
        }
        if (pwv !== pwv2) {
            pw2.focus();
            $("#error_pw2").text("两次输入密码不一致");
            return false;
        }
        return true;

    }

    function phoneCheck() {
        let phone = $("#phone");
        let pv = phone.val();
        if (pv.trim() < 11) {
            phone.focus();
            $("#error_phone").text("手机号长度不足");
            return false;
        } else {
            if (!(/^1[34578]\d{9}$/.test(pv))) {
                phone.focus();
                $("#error_phone").text("手机号格式不正确");
                return false;
            }
        }
        return fetchPhone();
    }
    
    function fetchUn() {
        let un = $("#un");
        let f = true;
        $.get("<c:url value="/asyn/username"/>", {username: un.val()}, (data)=> {
            if (data.status === true) {
                un.focus();
                $("#error_un").text("用户名已存在");
                f = false;
            }
        });
        return f;
    }
    
    function fetchPhone() {
        let phone = $("#phone");
        let f = true;
        $.get("<c:url value="/asyn/phone"/>", {phone: phone.val()}, (data)=> {
            if (data.status === true) {
                phone.focus();
                $("#error_phone").text("手机号已被使用");
                f = false;
            }
        });
        return f;
    }
</script>
</body>
</html>
